﻿using System;
using System.Collections.Generic;
using System.Text;
using NHibernate;
using NHibernate.Expression;
using Castle.ActiveRecord;
using Castle.ActiveRecord.Queries;

namespace Mang24k99.Model.KEYW
{
    [ActiveRecord(Table = "KT_KEYW_WORDS")]
    public partial class MKtKeywWords : ActiveRecordBase
    {
        private int _WordsNO;

        private IList<MKtKeywFixwords> _MKtKeywFixwords;
        private int _ID;
        private string _WordsName;
        private MKtKeywNewstraword _MKtKeywNewstraword;

        private int? _IsValidate;

        private DateTime? _InputTime;

        private DateTime? _ModifyTime;

        private DateTime? _DeleteTime;


        [BelongsTo("MAIN_WORD")]
        public MKtKeywNewstraword MKtKeywNewstraword
        {
            get
            {
                return _MKtKeywNewstraword;
            }
            set
            {
                _MKtKeywNewstraword = value;
            }
        }

        [HasMany(typeof(MKtKeywFixwords), Table = "KT_KEYW_FIXWORDS", ColumnKey = "WORDS_NO")]
        public IList<MKtKeywFixwords> MKtKeywFixwords
        {
            get
            {
                return _MKtKeywFixwords;
            }
            set
            {
                _MKtKeywFixwords = value;
            }
        }

        /// <summary>
        /// Gets or sets the WordsNO.()
        /// </summary>
        /// <value>The WordsNO.</value>
        [PrimaryKey(PrimaryKeyType.Assigned, "WORDS_NO")]
        public int WordsNO
        {
            get
            {
                return _WordsNO;
            }
            set
            {
                _WordsNO = value;
            }
        }


        /// <summary>
        /// Gets or sets the ID.()
        /// </summary>
        /// <value>The ID.</value>
        [Property("ID",Update = false, Insert = false)]
        public int ID
        {
            get
            {
                return _ID;
            }
            set
            {
                _ID = value;
            }
        }


        /// <summary>
        /// Gets or sets the WordsName.()
        /// </summary>
        /// <value>The WordsName.</value>
        [Property("WORDS_NAME")]
        public string WordsName
        {
            get
            {
                return _WordsName;
            }
            set
            {
                _WordsName = value;
            }
        }


        /// <summary>
        /// Gets or sets the IsValidate.()
        /// </summary>
        /// <value>The IsValidate.</value>
        [Property("IS_VALIDATE")]
        public int? IsValidate
        {
            get
            {
                return _IsValidate;
            }
            set
            {
                _IsValidate = value;
            }
        }


        /// <summary>
        /// Gets or sets the InputTime.()
        /// </summary>
        /// <value>The InputTime.</value>
        [Property("INPUT_TIME")]
        public DateTime? InputTime
        {
            get
            {
                return _InputTime;
            }
            set
            {
                _InputTime = value;
            }
        }


        /// <summary>
        /// Gets or sets the ModifyTime.()
        /// </summary>
        /// <value>The ModifyTime.</value>
        [Property("MODIFY_TIME")]
        public DateTime? ModifyTime
        {
            get
            {
                return _ModifyTime;
            }
            set
            {
                _ModifyTime = value;
            }
        }


        /// <summary>
        /// Gets or sets the DeleteTime.()
        /// </summary>
        /// <value>The DeleteTime.</value>
        [Property("DELETE_TIME")]
        public DateTime? DeleteTime
        {
            get
            {
                return _DeleteTime;
            }
            set
            {
                _DeleteTime = value;
            }
        }

        protected override void OnDelete()
        {
            MKtKeywWordsHistory mHistory = new MKtKeywWordsHistory();
            Common.ModelCommon.CopyObjectValues(this, mHistory);
            mHistory.MainWord = MKtKeywNewstraword.WordNO;
            mHistory.DeleteTime = DateTime.Now;
            mHistory.Create();
            base.OnDelete();
        }

        /// <summary>
        /// 创建一条记录
        /// </summary>
        /// <param name="keywords"></param>
        public static void Create(MKtKeywWords keywords)
        {
            keywords.Create();
        }

        /// <summary>
        /// 选择所有记录
        /// </summary>
        /// <returns></returns>
        public static IList<MKtKeywWords> SelectAll()
        {
            return (IList<MKtKeywWords>)FindAll(typeof(MKtKeywWords));
        }
        /// <summary>
        /// 物理删除一条记录，同时删除附表关联数据
        /// </summary>
        /// <param name="keyword"></param>
        public static void Delete(MKtKeywWords keyword)
        {
            for (int i = 0; i < keyword.MKtKeywFixwords.Count;i++ )
            {
                keyword.MKtKeywFixwords[i].Delete();
            }
            keyword.Delete();
        }
        /// <summary>
        /// 修改一条记录
        /// </summary>
        /// <param name="keyword"></param>
        public static void  Update(MKtKeywWords keyword)
        {
            keyword.Update();
        }
        /// <summary>
        /// 通过主键选择一条记录
        /// </summary>
        /// <param name="wordsno"></param>
        /// <returns></returns>
        public static MKtKeywWords SelectByPrimaryKey(int wordsno)
        {
            return (MKtKeywWords)FindByPrimaryKey(typeof(MKtKeywWords), wordsno);
        }

        public static MKtKeywWords SelectbyWordsNO(int wordsno)
        {
            string strhql = @" From MKtKeywWords keyword where keyword.WordsNO=?";
            ScalarQuery sq = new ScalarQuery(typeof(MKtKeywWords), strhql,wordsno);
            return (MKtKeywWords)ExecuteQuery(sq);
        }
        public static IList<MKtKeywWords> SelectALLByProperty(string property, object value)
        {
            return (IList<MKtKeywWords>)FindAllByProperty(typeof(MKtKeywWords), property, value);
        }
        public static IList<MKtKeywWords> SelectALLSliced(int firstResult, int maxResult, NHibernate.Expression.DetachedCriteria criteria)
        {

            return (IList<MKtKeywWords>)SlicedFindAll(typeof(MKtKeywWords), firstResult, maxResult, criteria);
        }
    }
}
